package com.androidified.personalshopper.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.androidified.personalshopper.R;
import com.androidified.personalshopper.bean.Discount;
import com.androidified.personalshopper.bean.Promotion;

public class PromotionAdapter extends ArrayAdapter<Promotion> {

	private static final int[] IMAGES_IDS = {
		R.drawable.offer1,
		R.drawable.offer2 };
	
	private LayoutInflater mInflater;
	
	public PromotionAdapter(Context context) {
		super(context, 0);
		mInflater = LayoutInflater.from(context);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final Promotion promotion = getItem(position);
		
		final ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.promotion_item, null);
			holder = new ViewHolder(convertView);
			convertView.setTag(holder);
			
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		if (holder.lkxa != null) {
			final int code = promotion.getId().codePointAt(promotion.getId().length() - 2);
			holder.lkxa.setVisibility((code % 2 == 1) ? View.VISIBLE : View.GONE);
		}
		if (holder.promotion_image != null) {
			final int code = promotion.getId().codePointAt(promotion.getId().length() - 1);
			holder.promotion_image.setImageResource(IMAGES_IDS[code % IMAGES_IDS.length]);
		}
		if (holder.promotion_text != null) {
			if (Discount.ABSOLUTE.equals(promotion.getDiscount().getDiscountType())) {
				holder.promotion_text.setText(promotion.getDiscount().getValue() + getContext().getString(R.string.promotion_absolute_short_text));
			} else if (Discount.PERCENTAGE.equals(promotion.getDiscount().getDiscountType())) {
				holder.promotion_text.setText(promotion.getDiscount().getValue() + getContext().getString(R.string.promotion_porcentage_short_text));
			}
		}
		if (holder.promotion_share != null) {
			
		}
		if (holder.promotion_favorite != null) {
			
		}
			
		return convertView;
	}
	
	private static class ViewHolder {
		private ImageView promotion_image;
		private TextView promotion_text;
		private View promotion_share;
		private View promotion_favorite;
		private View lkxa;
		
		public ViewHolder(View v) {
			promotion_image = (ImageView) v.findViewById(R.id.promotion_image);
			promotion_text = (TextView) v.findViewById(R.id.promotion_text);
			promotion_share = v.findViewById(R.id.promotion_share);
			promotion_favorite = v.findViewById(R.id.promotion_favorite);
			lkxa = v.findViewById(R.id.lkxa);
		}
	}
}
